﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class AccountUI : MonoBehaviour {

	public AppMgr m_AppMgr;
	public LoginUI m_LoginUI;
	public Button m_AccountBtn;

	public Text m_UserName;
	public Text m_Phone;
	public Text m_Age;
	public Text m_Sex;
	public Text m_BackLoginBtnText;

	// Use this for initialization
	void Start () {
		gameObject.SetActive(false);
		if (m_AppMgr.AccountInfo == null)
			m_BackLoginBtnText.text = "登录";
		else
			m_BackLoginBtnText.text = "注销";
	}

	private void OnEnable()
	{
		Reset();
	}

	private void Reset()
	{
		m_UserName.text = string.Empty;
		m_Phone.text = string.Empty;
		m_Age.text = string.Empty;
		m_Sex.text = string.Empty;
	}

	public void AccountBtn_Click()
	{
		m_AccountBtn.gameObject.SetActive(false);
		gameObject.SetActive(true);

		if (m_AppMgr == null || m_AppMgr.AccountInfo == null)
			return;

		m_UserName.text = m_AppMgr.AccountInfo.UserName;
		m_Phone.text = m_AppMgr.AccountInfo.Phone;
		m_Age.text = m_AppMgr.AccountInfo.Age.ToString();
		m_Sex.text = m_AppMgr.AccountInfo.Sex == 0 ? "男" : "女";
	}

	private void Update()
	{
		if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
		{
#if UNITY_ANDROID || UNITY_IPHONE
			if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
			if (EventSystem.current.IsPointerOverGameObject())
#endif
			{
				if (!EventSystem.current.currentSelectedGameObject.CompareTag(gameObject.tag))
					Close();
			}
			else
				Close();
		}
	}

	public void Close()
	{
		m_AccountBtn.gameObject.SetActive(true);
		gameObject.SetActive(false);
	}

	public void BackLoginBtn_Click()
	{
		m_AppMgr.AppData.PhoneNumber = string.Empty;
		m_AppMgr.AppData.Password = string.Empty;
		m_LoginUI.gameObject.SetActive(true);
		gameObject.SetActive(false);
	}
}
